<?php

/** 下家自动摸牌类
 * Class PM_n
 */
class PM_n
{
    public static function __index( $room_info , $this_uid , $chi_peng_fail , $db_connect , $send_uid = 888 , $card_number = 0 ){
        $next_card_number = PHZ_Tools::__get_next_number( $db_connect , $room_info['room_id'] );//获取下一张底牌
        //是否还有牌？没有牌了就做流局处理
        if( (int)$next_card_number <= 0 || $next_card_number == null || $next_card_number == "" ){
            //本局流局计分
            PHZ_Scoring::__game_Single_END( $room_info , 0 , 0 , 0 , 0 , 3 , $db_connect );
            return true;
        }
        $next_uid         = PHZ_User::__get_user_Next_userID( $room_info , $this_uid );//获取下一个自动摸牌用户的ID
        foreach( $room_info as $key => $value ) {
            if (strstr($key, 'room_player_') != false && $value != 0 ) {
                $data = array();
                $data['log_rid']            = $room_info['room_id'];
                $data['log_jid']            = $room_info['room_js'];
                $data['log_uid']            = $send_uid;//事件发送者
                $data['log_to_uid']         = $value;
                $data['log_md']             = 15;
                $temp = array();
                $temp['pl']                 = 8;
                $temp['card_number']        = $card_number;//有时候的下摸是建立在一个废牌的基础上
                $temp['put_uid']            = 0;//全部人丧失出牌权
                $temp['mop_uid']            = $next_uid;//摸牌权转移到下家
                $temp['next_card_number']   = $value != $next_uid ? 0 : $next_card_number;
                $temp['chi_peng_fail']      = $chi_peng_fail;//属于跑牌后的重跑免打字类型摸牌
                $temp['debug_log']          = isset( $room_info['debug_log'] ) && $room_info['debug_log'] != "" ? $room_info['debug_log'] : "";
                $data['log_data']           = json_encode( $temp );
                $db_connect->insert( PHZ_Log::__gTn( $room_info['room_id'] ) )->cols( $data )->query();
                PHZ_Card::__update_MAX_HeaderID( $room_info , $value , $db_connect );//更新用户的当前最大可执行ID
            }
        }
        //设置出牌权
        PHZ_Room::__setThisPutUser( $room_info['room_id'] , 0 , $db_connect );
        //设置当前房间摸牌人以及牌号 - 设置
        PHZ_Room::__setThisMopCard( $room_info['room_id'] , $next_uid , $next_card_number , false , $db_connect );
        //设置当前房间出牌人以及牌号 - 清空（只要是执行了摸牌；那么出牌人跟当前的出牌号肯定是要被清空的）
        PHZ_Room::__setThisPutCard( $room_info['room_id'] , 0 , 0 , true , $db_connect );
        //牌被摸上来以后，开始执行各项检查
        PHZ_Tools::__mopai_wei_ti_pao_chk( $room_info['room_id'] , 0 , $next_uid , $next_card_number , $db_connect );
    }
}